<?php
/**
 * 获取产品
 * Created by wzb.
 * great time:2017/08/12
 */
require_once("/home/ryerp_api/getorder/getorderproductExtensionAttr.php");
class Getproduct
{
   public $db;
/**
 * [byItemIdGetProduct 通过麦进斗的指定字段itemid 获取产品信息]
 * @param  [type] $Mazentop [接口]
 * @param  string $item_id  [description]
 * @return [type]           [description]
 */
public function byItemIdGetProduct($Mazentop=null,$item_id='',$increment_id='',$db,$store_id=''){
	//$Mazentop 46356 M000071764 $this->db 2
      $this->db=$db;
      $product_data = $Mazentop->getproductAttribute($item_id = "$item_id");
       echo "******************Start GET option id****************************\n";
       echo "\n\n"; print_r(['订单信息'=>$product_data]); echo "\n\n";
       /**获取产品属性信息开始*/
      $created_at = $product_data['created_at'];//产品下单时间
	  $name = $product_data['name'];//
	  $order_id = $product_data['order_id'];//
	  $original_price = $product_data['original_price'];//基础价格
	  $product_type = $product_data['product_type'];//产品组合类型
      $item_id = $product_data['item_id'];//
      $price = $product_data['price'];//产品价格
      $qty_ordered = $product_data['qty_ordered'];//订单数
      $sku = $product_data['sku'];//Sku
    echo "下单中的sku：$sku\n";
      $updated_at = $product_data['updated_at'];//更新时间
      if(isset($product_data['product_option'])){
        $product_option = $product_data['product_option']['extension_attributes']['custom_options'];//产品属性  如果不存在 有错误，不影响，先不改动
		  $option_arrary = array();
        if(is_array($product_option)){
          foreach ($product_option as $key => $value) {
                    $option_id = $value['option_id'];
                    $option_value = $value['option_value'];
                    $option_arrary["$option_id"] = $option_value;
          }
        }

      }else{
        $option_arrary=array();
      }
      $options_id = json_encode($option_arrary);
      if (empty($options_id)) {
        echo "操作id为空\n";
        $delete_statu_Y = "delete dlz_attribute_all_id where increment_id='$increment_id'";
        $checkStatus = $this->db->exec($delete_statu_Y);//如果出现 400 500，则不在推送。storeid
        return false;
      }
      //处理客户下单后的 sku 组合 
      $AnalyProduct = new AnalyProduct();
      //WT360W {10332:126474,10333:126474,10334:126478} R000030229 1 2 20076 182.7 $this->db
      $res = $AnalyProduct->Getpruductdetial($sku,$options_id,$increment_id,$pice_or_lotnums=$qty_ordered,$store_id,$item_id,$item_price=$price,$this->db);
      if (!$res) {
        return false;
      }
       echo "********************update options_id ******************** \n";
      $insert_option = "update dlz_attribute_all_id set options_id='$options_id',sync_status='0' where item_id='$item_id'";
      $result = $this->db->exec($insert_option);
      return true;
}

}



/**eg：
{
  "amount_refunded": 0,
  "applied_rule_ids": "22",
  "base_amount_refunded": 0,
  "base_discount_amount": 20,
  "base_discount_invoiced": 20,
  "base_discount_tax_compensation_amount": 0,
  "base_discount_tax_compensation_invoiced": 0,
  "base_original_price": 46,
  "base_price": 149.69,
  "base_price_incl_tax": 149.69,
  "base_row_invoiced": 149.69,
  "base_row_total": 149.69,
  "base_row_total_incl_tax": 149.69,
  "base_tax_amount": 0,
  "base_tax_invoiced": 0,
  "created_at": "2017-08-11 15:05:10",
  "discount_amount": 20,
  "discount_invoiced": 20,
  "discount_percent": 0,
  "free_shipping": 0,
  "discount_tax_compensation_amount": 0,
  "discount_tax_compensation_invoiced": 0,
  "is_qty_decimal": 0,
  "is_virtual": 0,
  "item_id": 445,
  "name": "Mslynn Brazilian Body Wave Hair Brazilian Virgin Hair 3 Bundles With Closure With Baby Hair",
  "no_discount": 0,
  "order_id": 384,
  "original_price": 46,
  "price": 149.69,
  "price_incl_tax": 149.69,
  "product_id": 263,
  "product_type": "simple",
  "qty_canceled": 0,
  "qty_invoiced": 1,
  "qty_ordered": 1,
  "qty_refunded": 0,
  "qty_shipped": 0,
  "quote_item_id": 5318,
  "row_invoiced": 149.69,
  "row_total": 149.69,
  "row_total_incl_tax": 149.69,
  "row_weight": 0.5,
  "sku": "BBW3P",
  "store_id": 1,
  "tax_amount": 0,
  "tax_invoiced": 0,
  "tax_percent": 0,
  "updated_at": "2017-08-11 15:05:10",
  "weee_tax_applied": "[]",
  "weight": 0.5,
  "product_option": {
    "extension_attributes": {
      "custom_options": [
        {
          "option_id": "3135",
          "option_value": "53486"
        },
        {
          "option_id": "3136",
          "option_value": "53494"
        },
        {
          "option_id": "3137",
          "option_value": "53514"
        }
      ]
    }
  }
}
Response Code
 */


//include("getorderproductExtensionAttr");
